/*
 * Created by zhangzxiang91@gmail.com on 2021/10/13.
 */
package com.perfma.util.model;

import lombok.Data;
import lombok.experimental.SuperBuilder;
import lombok.experimental.Tolerate;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.InputStream;

/**
 * 文件普通上传请求
 *
 * @author zhangzxiang91@gmail.com
 * @date 2021/10/13.
 */
@Data
@SuperBuilder
public class UploadRequest {

	/**
	 * 上传文件Key，如果未指定则自动生成
	 */
	private String fileKey;

	/**
	 * 上传文件路径
	 */
	private File uploadFile;

	/**
	 * 上传文件输入流
	 * <p>
	 * 仅支持最大{@link Integer#MAX_VALUE}(2G)的文件输出流上传，因为{@link InputStream#available()}最大只能表示{@link Integer#MAX_VALUE}大小的字节
	 * 对于大小超过2G的文件，建议通过{@link UploadRequest#uploadFile}的方式上传
	 */
	private InputStream uploadStream;

	/**
	 * 上传文件字节数据
	 */
	private byte[] uploadBytes;

	@Tolerate
	public UploadRequest() {}

	@Override
	public String toString() {
		return "UploadRequest{" + toSourceString() + '}';
	}

	protected String toSourceString() {
		String str = "";
		if (uploadFile != null) {
			str += "file=" + uploadFile.getAbsolutePath() + " ,";
		}
		if (uploadStream != null) {
			str += "stream=" + uploadStream.getClass() + " ,";
		}
		if (uploadBytes != null) {
			str += "bytes=<" + uploadBytes.length + " bytes> ,";
		}
		return StringUtils.removeEnd(str, " ,");
	}

}

